package org.jwcloud.project.module.zx.template.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.jwcloud.project.module.zx.common.FmtUtil;
import org.jwcloud.project.module.zx.common.Resp;
import org.jwcloud.project.module.zx.common.repository.RptCommonRepository;
import org.jwcloud.project.module.zx.common.service.RptUtService;
import org.jwcloud.project.module.zx.template.service.RptSpService;
import org.jwcloud.project.module.zx.template.service.RptTemplateService;
import org.jwcloud.project.module.zx.template.service.repository.RptManTipRepository;
import org.jwcloud.project.module.zx.template.service.repository.RptSpRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jw.mid.mm.comm.common.controller.BaseController;
import com.jw.mid.mm.comm.common.controller.CrudController;
import com.jw.mid.mm.comm.common.json.Response;
import com.jw.mid.mm.comm.common.pagination.ListBounds;
import com.jw.mid.mm.comm.common.pagination.ListSlice;
import com.jw.mid.mm.comm.common.pagination.PageListSlice;
import com.jw.mid.mm.comm.common.pagination.Pagination;
import com.jw.mid.mm.comm.common.service.CrudService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value = "API - Controlle",
description = "手动督办", tags = "manTip")
@RestController
@RequestMapping("/rest/rptManTip")
public class RptManTipController extends BaseController{
	
//	@Autowired
//    private CommonService commonService;
	
	@Autowired
	public RptCommonRepository rptCommonRepository;
	
	@Autowired
	public RptManTipRepository rptManTipRepository;
	
	@Autowired
    public RptUtService rptUtService;
	
	@Autowired
    public RptSpService rptSpService;
	
	@PostMapping("insertDb")
    @ApiOperation(value = "领导添加督办信息", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "预警id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "ld_content", value = "纪委领导督办内容", required = false, dataType = "string", paramType = "query"),
    })
    public Resp insertDb(
    		Pagination pagination,
    		@RequestBody(required = false) Map<String,String> param
    		){
		String flag = FmtUtil.nullRes(param
				, "id", "id"
				, "ld_content", "督办内容");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
        
		
		int res = this.rptManTipRepository.insertDb(param);
		 
		if(res == 0) {
			return Resp.error("重复操作");
		}else {
			return Resp.success(1);
		}
    }
	
	@PostMapping("insertDb_fish")
    @ApiOperation(value = "领导添加督办信息（鱼骨图专用）", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "取自pro_task_info_id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "ld_content", value = "纪委领导督办内容", required = false, dataType = "string", paramType = "query"),
    })
    public Resp insertDb_fish(
    		@RequestBody(required = false) Map<String,String> param
    		){
		String flag = FmtUtil.nullRes(param
				, "id", "id"
				, "ld_content", "督办内容");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
        
		int res = this.rptManTipRepository.insertDb_fish(param);
				
		if(res == 0) {
			return Resp.error("重复操作");
		}else {
			return Resp.success(1);
		}
		
    }
	
	@GetMapping("queryDbList")
    @ApiOperation(value = "督办列表", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "project_name", value = "项目名称", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "work_name", value = "工作名称", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "task_name", value = "任务名称", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "duty_dept", value = "责任部门", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "db_state", value = "督办状态", required = false, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "fk_duty_person", value = "反馈里的责任人", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "db_time_start", value = "上级督办时间 开始", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "db_time_end", value = "上级督办时间 结束", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "xf_time_start", value = "下发时间 开始", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "xf_time_end", value = "下发时间 结束", required = false, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "pageType", value = "上级督办sjdb 督办下级dbxj 下级反馈xjfk", required = false, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "reply_flag", value = "督办信息查询 1已反馈 0未反馈", required = false, dataType = "string", paramType = "query"),
    	
    })
    public Resp queryDbList(
    		Pagination pagination,
    		@RequestParam(required = false) Map<String,String> param
    		){
		
		FmtUtil.pagiBuild(pagination, param);
		
		//System.out.println("after build: " + param);
        return Resp.success(this.rptUtService.findPagi(
    		pagination,
    		param,
    		this.rptManTipRepository.queryDbList(param), 
    		this.rptManTipRepository.queryDbList_total(param)
    	));
    }
	
	@PostMapping("doXf")
    @ApiOperation(value = "下发", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "预警id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "done_person", value = "指定责任人", required = false, dataType = "string", paramType = "query"),
    })
    public Resp doXf(
    		@RequestBody(required = false) Map<String,String> param
    		){
		String flag = FmtUtil.nullRes(param
				, "id", "id"
				, "done_person", "指定责任人");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
        
		return Resp.success(this.rptManTipRepository.doXf(param));
    }
	
	@PostMapping("doTip")
    @ApiOperation(value = "再次提醒", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    })
    public Resp doTip(
    		@RequestBody(required = false) Map<String,String> param
    		){
		String flag = FmtUtil.nullRes(param
				, "id", "id");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
        
		return Resp.success(this.rptManTipRepository.doTip(param));
    }
	
	@PostMapping("doFk_xj")
    @ApiOperation(value = "反馈-下级（督办反馈）", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "reason_type", value = "任务超时原因", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "fk_duty_person", value = "责任人", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "overtime_content", value = "超时具体原因", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "handle_content", value = "整改措施", required = false, dataType = "string", paramType = "query"),
    })
    public Resp doFk_xj(
    		@RequestBody(required = false) Map<String,String> param
    		){
		String flag = FmtUtil.nullRes(param
				, "reason_type", "任务超时原因"
				, "overtime_content", "超时具体原因"
				, "handle_content", "整改措施");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
		return Resp.success(this.rptManTipRepository.doFk_xj(param));
    }
	
	@PostMapping("doFk_sj")
    @ApiOperation(value = "反馈-上级（任务督办-下发）", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "reason_type", value = "任务超时原因", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "fk_duty_person", value = "责任人", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "overtime_content", value = "超时具体原因", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "handle_content", value = "整改措施", required = false, dataType = "string", paramType = "query"),
    })
    public Resp doFk_sj(
    		@RequestBody(required = false) Map<String,String> param
    		){
		String flag = FmtUtil.nullRes(param
				, "reason_type", "任务超时原因"
				, "overtime_content", "超时具体原因"
				, "handle_content", "整改措施");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
        
		return Resp.success(this.rptManTipRepository.doFk_sj(param));
    }
	
}